package com.example.demo2.service;

import com.example.demo2.util.RabbitMqUtils;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class TTLDirectService {
    @Autowired
    RabbitTemplate rabbitTemplate;
    public void sendOrder(String name,String thing,int num){
        String uuid= UUID.randomUUID().toString();
        System.out.println("订单生成成功"+uuid);
        String message="ttl-测试";
        MessagePostProcessor messagePostProcessor=new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("5000");
                message.getMessageProperties().setContentEncoding("utf-8");
                return message;
            }
        };
        //通过MQ来实现消息的分发
        for (int i = 0; i < 26; i++) {
            rabbitTemplate.convertAndSend(RabbitMqUtils.TTLORDER_EXCHANGE,"ttl",message+uuid);
        }

    }
}
